package com.yc.fav.web;

import com.google.gson.Gson;
import com.yc.fav.bean.User;
import com.yc.fav.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/findUser")
public class FindUserServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //
        ServletContext servletContext = this.getServletContext();
        ApplicationContext cxt = (ApplicationContext) servletContext.getAttribute("cxt");
        UserMapper userMapper = cxt.getBean(UserMapper.class);
        User user = userMapper.selectById(1);
        resp.setContentType("application/json;charset=utf-8");
        Gson gson=new Gson();
        String json = gson.toJson(user);
        resp.getWriter().println(json);
    }
}
